import Const from './const';
import './sysext';

const ObjectUitls = {
    minerAssign(dest, ...srcs) {
        let fs = Object.keys(dest);
        for (let src of srcs) {
            for (let f of fs) {
                if (src[f] != null) {
                    dest[f] = src[f];
                }
            }
        }
        return dest;
    },
    deepEquals(l, r,  { 
        compare = (l, r) => l === r, 
        maxDepth = Const.MaxDeepEqualDepth,
        depth = 1,
        include = [],
        exclude = []
    } = {}) {
        if (depth > maxDepth) {
            return true;
        } else if (l === r) {
            return true;
        } else if (typeof l === 'object' && typeof r === 'object' && l !== null && r !== null) {
            let lk = Object.keys(l);
            let rk = Object.keys(r);
            if (lk.length !== rk.length) {
                return false;
            }
            return lk.filter(k => (include.length === 0 || include.includes(k)) && !exclude.includes(k))
                .every(k => ObjectUitls.deepEquals(l[k], r[k], { compare, maxDepth, depth: depth + 1 }));
        } else {
            return compare(l, r);
        }
    },
    copy(obj, options) {
        if (obj == null) {
            return obj;
        }
        return obj.copy(options);
    },
    deepCopy(obj, options) {
        if (obj == null) {
            return obj;
        }
        return obj.deepCopy(options);
    }
};

export default ObjectUitls;